<template>
	<view class="content">
		<text style="margin-left: 37%;">近3个月分析图表（合并）</text>
		<view>
			<view class="mx-2" style="height: 500rpx;">
				<qiun-data-charts type="mix" :opts="opts" :chartData="chartData" />
			</view>
		</view>
		<!-- 表格 -->
		<view class="table">
			<view class="tr font-weight-bold">
				<view class="th font">月份</view>
				<view class="th font">销售收入</view>
				<view class="th font">利润</view>
				<view class="th font">利润率</view>
			</view>
			<view class="tr font " v-for="(item, index) in chartData.categories" :key="index">
				<view class="td font">{{ item }}</view>
				<view class="td font">{{ getChartDataByName('销售输入')[index] }}元</view>
				<view class="td font">{{ getChartDataByName('利润')[index] }}元</view>
				<view class="td font">{{ getChartDataByName('利润率')[index] }}%</view>
			</view>
		</view>
	</view>
</template>
<script>
	const app = getApp();
	import {WeChatProfitAnalysis} from '@/api/report_forms.js';
	export default {
		data() {
			return {
				chartData: {},
				opts: {
					color: ["#1890FF", "#91CB74", "#FAC858", "#EE6666", "#73C0DE", "#3CA272", "#FC8452", "#9A60B4",
						"#ea7ccc"
					],
					title: '图表（合并）',
					padding: [10, 20, 0, 0],
					enableScroll: false,
					legend: {},
					xAxis: {
						disableGrid: true,
						title: "单位:月"
					},
					yAxis: {
						disabled: false,
						disableGrid: false,
						splitNumber: 5,
						gridType: "dash",
						dashLength: 4,
						gridColor: "#CCCCCC",
						padding: 10,
						showTitle: true,
						data: [{
								position: "right",
								title:'利润率',
								min: 0,
							},
							{
								position: "left",
								min: 0,
								title: "销售额",
								textAlign: "left"
							}
						]
					},
					extra: {
						mix: {
							column: {
								width: 20
							}
						}
					}
				}
			};
		},
		onReady() {
			this.getServerData();
		},
		methods: {
			async getServerData() {
				//模拟从服务器获取数据时的延时
				const res = await WeChatProfitAnalysis({Enterprise_Info_SerialNumber:'E00000000000016'});
				console.log(res);
				setTimeout(() => {
					//模拟服务器返回数据，如果数据格式和标准格式不同，需自行按下面的格式拼接
					let res = {
						categories: ["1", "2", "3"],
						series: [{
								name: "销售输入",
								index: 1,
								type: "column",
								data: [1110, 1240, 588]
							},
							{
								name: "利润",
								index: 1,
								type: "column",
								data: [1650, 1314, 838]
							},
							{
								name: "利润率",
								type: "line",
								color: "#2fc25b",
								data: [20, 10, 40]
							},
						]
					};
					this.chartData = JSON.parse(JSON.stringify(res));
				}, 500);
			},
			getChartDataByName(name) {
				if (this.chartData && this.chartData.series) {
					const series = this.chartData.series.find((item) => item.name === name);
					if (series) {
						return series.data;
					}
				}
				return [];
			}
		}
	};
</script>

<style scoped>
	.content {
		width: 100%;
		height: 100%;
	}
	
	.table {
		margin-top: 30px;
		width: 90%;
		margin: auto;
		border-radius: 8rpx;
		display: table;
		border: 1px solid #f7f7f7;
		border-collapse: collapse;
	}
	
	.th {
		text-align: center;
		color: #FFFFFF;
		padding: 20rpx 0;
		font-weight: bolder;
		display: table-cell;
		border: 1px solid #f7f7f7;
		background-color: #f86f10;
		font-size: 15px;
	}
	
	.td {
		text-align: center;
		background: #FFFFFF;
		padding: 20rpx 0;
		display: table-cell;
		border: 1px solid #f7f7f7
	}
	
	.tr {
		display: table-row;
	}
</style>